package com.thread.laoma;

/**
 * Created by zhuangqi on 17-4-8.
 */
public class AssenblePointDemo {

    static class PointSignal{
        private Integer tourists_num ;

        public PointSignal(Integer tourists_num) {
            this.tourists_num = tourists_num;
        }

        public synchronized void await(){
            if (tourists_num > 0){
                tourists_num --;
                if (tourists_num == 0){
                    notifyAll();
                }else {
                    await();
                }
            }
        }

    }

    static class Tourists extends Thread{
        private PointSignal pointSignal;

        public Tourists(PointSignal pointSignal) {
            this.pointSignal = pointSignal;
        }

        @Override
        public void run() {
            try {
                Thread.sleep((int) (Math.random() * 1000));
                pointSignal.await();
                System.out.println("集合完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        int num = 10;
        Thread[] threads = new Tourists[num];
        PointSignal p = new PointSignal(num);
        for (int i = 0; i < num; i++) {
            threads[i] = new Tourists(p);
            threads[i].start();
        }
    }

}
